/**
 * Created by liuqingxia on 2016/4/11.
 */

var count = 0;// 进度条计数器
var intervalId;// 循环方法Id
var progressId = "#progressBar";
var progressBarDivId = "#progressBarDiv";

var token = true;// 防止重复提交

$(document).ready(
		function() {
			var allDataSourceDiv = getAllDataSourceDiv("datasourceSelect",
					"/datasource/findAll", "master");
			allDataSourceDiv.init();

			clickSourceInsertBtn("selectedDataSource-div", "master");
			getDbKey();
			clickSourceRemoveBtn();
			clickGroupInsertCommitBtn();
			isLocalRadioClickEvent();
		});

/**
 * 进度条滚动...
 */
function progressing() {
	count = count + 1;
	if (count > 100) {
		window.clearInterval(intervalId);
		count = 0;
		return;
	}
	$(progressId).css("width", count + "%");
}

/**
 * 进度条滚动
 */
function setIntervalProgressBar() {
	intervalId = self.setInterval("progressing()", 100);
}

/**
 * 进度条显示
 */
function showProgressBar() {
	$(progressBarDivId).show();
}

/**
 * 隐藏进度条
 */
function hideProgressBar() {
	$(progressBarDivId).hide();
}

/**
 * 清空进度条
 */
function clearProgressBar() {
	window.clearInterval(intervalId);
	$(progressId).css("width", "0%");
}

/**
 * 完成进度条
 */
function fullProgressBar() {
	window.clearInterval(intervalId);
	count = 0;
	$(progressId).css("width", "100%");
}

/**
 * 点击AppInsert的提交按钮
 */
function clickGroupInsertCommitBtn() {
	$(document).on("click", "#atomInsertCommit-btn", function() {
		var flag = true;
		var url = getUrlBySuffix("/tddl/atom/saveAtom");
		var params = getParams();
		for ( var p in params) {
			if (p != "enckey") {
				if (isNull(params[p]) || params[p] == false) {
					swal("警告", "表单内容有误请检查", 'warning');
					flag = false;
					return;
				}
			}
		}

		showProgressBar();
	    setIntervalProgressBar();

		// console.log(JSON.stringify(params));
		if (token && flag) {
			console.log(params);
			$.postHttp(params, url, function(json) {
				if (json && json.success) {
					fullProgressBar();
					swal("成功", json['message'], "success");
					token = false;
				} else {
					swal("警告", json['message'], "error");
				}
				hideProgressBar();
				clearProgressBar();
			});
		}

	});
}

function getDbKey(){
	$(document).on("change","#datasourceSelect",function(){
		var thisChange=$(this).find("option:selected");
		var dbname=$(thisChange).attr("databasename");
		$("input[name=dbkey]").val(dbname);
	});
}
function getParams() {
	var param = {};
	param.dataSourceInfo = getDataSourceInfo().toString();
	param.appDataSourceInfo = $("#app-atom-info").val();
	param.appName = $.trim($("input[name=appName]").val());
	param.dbkey = $.trim($("input[name=dbkey]").val());
	param.enckey = getEnckey();
	param.projectGroup = getProjectGroup();
	return param;
}
function getEnckey() {
	var islocalValue = $("input[name=islocal]:checked").val();
	if (islocalValue == "0") {
		return null;
	} else {
		return $.trim($("input[name=enckey]").val());
	}
}
function getDataSourceInfo() {
	var datasourceOpt = $("#datasourceSelect").find("option:selected");
	if ($(datasourceOpt).html() == "请选择数据源") {
		return false;
	}
	var dataSourceInfo = '{"databaseIp":"' + datasourceOpt.attr('databaseip')
			+ '","databasePort":"' + datasourceOpt.attr('databasePort')
			+ '","databaseType":"' + datasourceOpt.attr('databaseType')
			+ '","databaseName":"' + datasourceOpt.attr('databaseName')
			+ '","databaseId":"' + datasourceOpt.attr('databaseid') + '"}';
	return dataSourceInfo;

}

function getProjectGroup() {
	var projectGroup = $(".projectName").find("option:selected");
	if ($(projectGroup).html() == "请选择项目组") {
		return false;
	}

	return $(projectGroup).val();

}

function getDBlist() {
	var dblist = "[";
	var dataSources = $("#selectedDataSource-div").find(".dataSource-row");
	if (dataSources.length == 0) {
		swal("警告", "请选择一个数据源", "error");
		return false;
	}
	var count = 0;
	for (var i = 0; i < dataSources.length; i++) {

		var dataSource = dataSources[i];
		var dbName = $.trim($(dataSource).find("input[name=dbname]").val());
		var readWeight = Number($.trim($(dataSource).find(
				"input[name=readWeight]").val()));
		var writeWeight = Number($.trim($(dataSource).find(
				"input[name=writeWeight]").val()));
		var datasourceId = $.trim($(dataSource).find("input[name=dbid]").val());
		var jdbcUrl = $.trim($(dataSource).find("input[name=dbJdbcUrl]").val());
		if (readWeight == 0 && writeWeight == 0) {
			swal("警告", "请选择读权重和写权重不可都为0", "error");
			count = 0;
			break;
		}
		var rowValue = '{"dbName":"' + dbName + '","readWeight":"' + readWeight
				+ '","writeWeight":"' + writeWeight + '","datasourceId":"'
				+ datasourceId + '","jdbcUrl":"' + jdbcUrl + '"},';
		dblist += rowValue;
		count++;
	}
	if (count == 0) {
		return false;
	}
	dblist = dblist.substring(0, dblist.lastIndexOf(","));
	dblist += "]";
	return dblist;
}

function isLocalRadioClickEvent() {
	$(document).on("click", "input[name=isLocal]", function() {
		var thisValue = $(this).val();
		if (thisValue == "1") {
			$(".localEnckey").show();
		} else {
			$(".localEnckey").hide();
		}
	})
}
